<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>FooTable</title>

    <link rel="shortcut icon" href="favicon.ico"> <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="css/plugins/footable/footable.core.css" rel="stylesheet">

    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">

</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-title">
						<h5>用户查询</h5>
					</div>
					<div class="ibox-content">
						<div class="col-sm-12">
							<div class="form-horizontal">
								<!--p>新增行政区域</p-->
								<div class="form-group">
									<div class="col-sm-4">
										<input id="form-personnelName" placeholder="用户名称" class="form-control">
									</div>
									<div class="col-sm-4">
										<button id="search-submit" class="btn btn-primary">查询</button>
									</div>
								</div>
							</div>
						</div>

						<table id="personnelTable" class="table">
							<thead>
							<tr>
								<th>用户ID</th>
								<th>用户名</th>
								<th>密码</th>
								<th>中文名</th>
								<th>所在单位</th>
								<th>角色</th>
								<th>手机号</th>
								<th>权限</th>
								<th>修改</th>
								<th>删除</th>
							</tr>
							</thead>
							<tbody>
							</tbody>
							<tfoot>
							</tfoot>
						</table>
						<div>
							<button id="prevButton" type="button" class="btn btn-primary">上一页</button>
							<button id="nextButton" type="button" class="btn btn-primary">下一页</button>
						</div>
					</div>
					<div id="permissionTable" class="ibox float-e-margins">
						<div class="ibox-title">
							<h5>权限修改 <span id="currentPermission"></span></h5>
						</div>
						<div class="ibox-content">
							<p>默认角色权限</p>
							<table id="RolePermissionTable" class="table">
								<thead>
								<tr>
									<th>权限id</th>
									<th>权限内容</th>
									<th>权限</th>
									<th></th>
								</tr>
								</thead>
								<tbody>
								</tbody>
								<tfoot>
								</tfoot>
							</table>
							<p>角色单独权限</p>
							<table id="PersonnelPermissionTable" class="table">
								<thead>
								<tr>
									<th>权限id</th>
									<th>权限内容</th>
									<th>权限</th>
									<th></th>
								</tr>
								</thead>
								<tbody>
								</tbody>
								<tfoot>
								</tfoot>
							</table>
							<form class="form-horizontal">
								<div class="form-group">
									<div class="col-sm-2">
										<button id="permissions-submit" type="button" class="btn btn-primary">确认权限修改</button>
									</div>
								</div>
							</form>
						</div>
					</div>
				
					
					<div id="modify-form" class="ibox float-e-margins">
						<div class="ibox-title">
							<h5>修改用户信息</h5>
						</div>
						
						<div class="ibox-content">
							<form class="form-horizontal">
								<!--p>新增行政区域</p-->
								<div class="form-group">
									<label class="col-sm-2 control-label">用户id：</label>
									<div class="col-sm-4">
										<input id="modify-personnelId" readonly="readonly" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">用户名：</label>
									<div class="col-sm-4">
										<input id="modify-personnelCode" placeholder="用户名" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">密码：</label>
									<div class="col-sm-4">
										<input id="modify-personnelPassword" placeholder="密码" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">中文名：</label>
									<div class="col-sm-4">
										<input id="modify-personnelPostName" placeholder="中文名" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">所在单位：</label>
									<div class="col-sm-4">
										<input id="modify-personnelCompany" placeholder="所在单位" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">角色：</label>
									<div class="col-sm-4">
										<input id="modify-personnelRole" placeholder="角色" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">手机号：</label>
									<div class="col-sm-4">
										<input id="modify-personnelTel" placeholder="手机号" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<div class="col-sm-2">
										<button id="modify-submit" class="btn btn-primary">确认修改</button>
									</div>
								</div>
							</form>
						</div>
					</div>
			
					<div class="ibox float-e-margins">
						<div class="ibox-title">
							<h5>新增用户</h5>
							<div class="ibox-content">
									<button id="addButton" class="btn btn-primary">新增</button>
							</div>
						</div>
						
						<div id="add-form" class="ibox-content">
							<form class="form-horizontal">
								<!--p>新增行政区域</p-->
								<div class="form-group">
									<label class="col-sm-2 control-label">用户名：</label>
									<div class="col-sm-4">
										<input id="add-personnelCode" placeholder="用户名" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">密码：</label>
									<div class="col-sm-4">
										<input id="add-personnelPassword" placeholder="密码" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">中文名：</label>
									<div class="col-sm-4">
										<input id="add-personnelPostName" placeholder="中文名" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">所在单位：</label>
									<div class="col-sm-4">
										<input id="add-personnelCompany" placeholder="所在单位" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">角色：</label>
									<div class="col-sm-4">
										<input id="add-personnelRole" placeholder="角色" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">手机号：</label>
									<div class="col-sm-4">
										<input id="add-personnelTel" placeholder="手机号" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<div class="col-sm-offset-2 col-sm-4">
										<button id="add-submit" class="btn btn-primary">确认新增</button>
									</div>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	
    <!-- 全局js -->
    <script src="js/jquery.min.js?v=2.1.4"></script>
    <script src="js/bootstrap.min.js?v=3.3.6"></script>
    <script src="js/plugins/footable/footable.all.min.js"></script>
    <!-- 自定义js -->
    <!--script src="js/content.js?v=1.0.0"></script-->
	<script src="js/service/service.js"></script>
    <script>
		let currPage;
		let PageSize;
		let UserID;
		let RoleID;
        $(document).ready(function() {
			currPage = 1
			PageSize = 5
			PermissionRole = 0
			$("div#add-form").hide()
			$('#modify-form').hide()
			$("div#permissionsTable").hide()
			$("div#personnelTable").hide()
        });
		
		$('.footable').footable();
		
		$('#search-submit').click(function(){
			currName = $('#form-personnelName').val()
			if(currName != ""){
				$("#personnelTable tbody").html("")
				service.getPersonnelBypage(currPage, PageSize, currName, fillPersonnel, console.log);
			}
		});
		
		function fillPersonnel(obj){
			var totalpage = JSON.parse(obj)["totalpage"]
			if(currPage == totalpage || totalpage == 0){
				$('#nextButton').hide()
			}else{
				$('#nextButton').show()
			}
			
			if(currPage == 1 || totalpage == 0){
				$('#prevButton').hide()
			}else{
				$('#prevButton').show()
			}
		
			var objValue = JSON.parse(obj)["data"]
			var table = document.getElementsByTagName("tbody")[0];
			console.log(objValue)
			for(var ele in objValue){
				var tr = table.insertRow(table.rows.length);
				tr.insertCell(0).innerHTML = objValue[ele]["id"]
				tr.insertCell(1).innerHTML = objValue[ele]["personnelCode"]
				tr.insertCell(2).innerHTML = objValue[ele]["personnelPassword"]
				tr.insertCell(3).innerHTML = objValue[ele]["postName"]
				tr.insertCell(4).innerHTML = objValue[ele]["company"]["id"]
				tr.insertCell(5).innerHTML = objValue[ele]["roleCode"]
				tr.insertCell(6).innerHTML = objValue[ele]["tel"]
				tr.insertCell(7).innerHTML = '<a onclick="permission(' + objValue[ele]["id"] +','+ objValue[ele]["roleCode"] +')"><i class="fa fa-wrench"></i> 权限</a>';
				tr.insertCell(8).innerHTML = '<a onclick="modify(this,' + objValue[ele]["id"] +')"><i class="fa fa-wrench"></i> 修改</a>';
				tr.insertCell(9).innerHTML = '<a onclick="remove(this,' + objValue[ele]["id"] +')"><i class="fa fa-wrench"></i> 删除</a>';
			}	
		}

		function permission(id, rid){
			$("div#RolePermissionTable").show()
			$("div#PersonnelPermissionTable").show()

			UserID = id
			RoleID = rid
			service.getPermission(RoleID, fillRolePermission, console.log)
			service.getPersonnelPermission(UserID, fillPersonnelPermission, console.log)
		}
		
		function fillRolePermission(obj){
			$("#RolePermissionTable tbody").html("")
			var objValue = JSON.parse(obj)["data"]
			var table = document.getElementsByTagName("tbody")[1];
			for(var ele in objValue){
				if(objValue[ele]["funOrMenu"] == 2){
					var tr = table.insertRow(table.rows.length);
					tr.insertCell(0).innerHTML = objValue[ele]["id"]
					tr.insertCell(1).innerHTML = objValue[ele]["menuName"]
					if(objValue[ele]["select"] == true){
						tr.insertCell(2).innerHTML = "有权限"
					}else{
						tr.insertCell(2).innerHTML = "没有权限"
					}
				}
			}
		}
		
		function fillPersonnelPermission(obj){
			$("#PersonnelPermissionTable tbody").html("")
			var index = 0
			var objValue = JSON.parse(obj)["rolepermission"]
			var table = document.getElementsByTagName("tbody")[2];
			console.log(objValue)
			for(var ele in objValue){
				if(objValue[ele]["funOrMenu"] == 2){
					index++;
					var tr = table.insertRow(table.rows.length);
					tr.insertCell(0).innerHTML = objValue[ele]["id"]
					tr.insertCell(1).innerHTML = objValue[ele]["menuName"]
					tr.insertCell(2).innerHTML = '<div class="radio"><label><input type="radio" value="true" id="perRadioTrue'+ index +'" name="per'+ index +'">有权限</label><label><input type="radio" value="false" id="perRadioFalse'+ index +'" name="per'+ index +'" >没有权限</label></div>';
					if(objValue[ele]["select"] == true){
						$('#perRadioTrue'+index).prop('checked', true)
					}else{
						$('#perRadioFalse'+index).prop('checked', true)
					}
				}
			}
		}
		
		$('#permissions-submit').click(function(){
			var list = "(["
			var tb = document.getElementById('PersonnelPermissionTable');
			var rows = tb.rows;
			for(var i = 1; i<rows.length; i++ ){
				var id = rows[i].cells[0].innerHTML
				var select = $('#PersonnelPermissionTable input[name="per' + i +'"]:checked ').val()
				list += "{id:"+id+",select:"+select+"}"
				if(i < rows.length-1)
					list += ","
			}
			list += "])"
			console.log(eval(list))
			service.modifyPersonnelPermission(UserID, eval(list), help, console.log)
		});
		
		function help(obj){
			console.log(obj)
			alert('操作成功')
		}
		
		function remove(cell,id){
			service.deletePersonnel(id, help, console.log)
			var tr=cell.parentNode.parentNode;
			var table=document.getElementsByTagName("table")[0]
			table.deleteRow(tr.rowIndex);
		}
		
		function modify(cell, id){
			$('#modify-form').show()
			var tr = cell.parentNode.parentNode;
			$('#modify-personnelId').attr('value',tr.cells[0].innerText)
			$('#modify-personnelCode').attr('value',tr.cells[1].innerText)
			$('#modify-personnelPassword').attr('value',tr.cells[2].innerText)
			$('#modify-personnelPostName').attr('value',tr.cells[3].innerText)
			$('#modify-personnelCompany').attr('value',tr.cells[4].innerText)
			$('#modify-personnelRole').attr('value',tr.cells[5].innerText)
			$('#modify-personnelTel').attr('value',tr.cells[6].innerText)
		}
		
		$('#modify-submit').click(function(){
			var id, code, passwd, name, pcode, role, tel
			id = $('#modify-personnelId').val()
			code = $('#modify-personnelCode').val()
			passwd = $('#modify-personnelPassword').val()
			name = $('#modify-personnelPostName').val()
			pcode = $('#modify-personnelCompany').val()
			role = $('#modify-personnelRole').val()
			tel = $('#modify-personnelTel').val()
			
			if(code == "" || passwd == "" || name == "" || pcode == "" || role == "" || tel == ""){
				alert("请完整填写内容")
			}else{
				service.modifyPersonnel(id, code, passwd, name, pcode, tel, role, help, console.log)
			}
		});
		
		$('#add-submit').click(function(){
			var code, passwd, name, pcode, role, tel
			code = $('#add-personnelCode').val()
			passwd = $('#add-personnelPassword').val()
			name = $('#add-personnelPostName').val()
			pcode = $('#add-personnelCompany').val()
			role = $('#add-personnelRole').val()
			tel = $('#add-personnelTel').val()
			
			if(code == "" || passwd == "" || name == "" || pcode == "" || role == "" || tel == ""){
				alert("请完整填写内容")
			}else{
				service.addPersonnel(code, passwd, name, pcode, tel, role, help, console.log)
			}
		});
		
		
		$('#nextButton').click(function(){
			$("#roleTable tbody").html("")
			currPage = currPage + 1
			service.getPersonnelBypage(currPage, PageSize, fillPersonnel, console.log)
		});
		
		$('#prevButton').click(function(){
			$("#roleTable tbody").html("")
			currPage = currPage - 1
			service.getPersonnelBypage(currPage, PageSize, fillPersonnel, console.log)
		});
		
		$('#addButton').click(function(){
			if($('div#add-form').is(':hidden')){
				$('div#add-form').show()
				$('#addButton').text("取消新增")
			}else{
				$('div#add-form').hide()
				$('#addButton').text("新增")
			}
		});
		
		
    </script>

    <script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>
    <!--统计代码，可删除-->

</body>

</html>
